﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CNFProblem
{
    /// <summary>
    /// Interaction logic for BuildLiteral.xaml
    /// </summary>
    public partial class BuildLiteral : UserControl
    {
        public BuildLiteral()
        {
            InitializeComponent();
        }

        private void _addBtn_Click(object sender, RoutedEventArgs e)
        {
            var la = new OneLiteral();
            la.Remove += (s, ev) => _items.Children.Remove((UIElement)s);
            _items.Children.Add(la);
        }

        private void _clearBtn_Click(object sender, RoutedEventArgs e)
        {
            _items.Children.Clear();
        }

        public void BuildFromFormula(CNF.LogicalFormula3CNF formula)
        {
            List<List<CNF.VariableRepresentation>> representations = formula.GetVariableRepresentations();
            foreach (List<CNF.VariableRepresentation> representation in representations)
            {
                OneLiteral la = new OneLiteral();
                la.SetVariables(representation);
                la.Remove += (s, ev) => _items.Children.Remove((UIElement)s);
                _items.Children.Add(la);
            }
        }

        public string Literal
        {
            get
            {
                string str = "";
                foreach (var item in _items.Children.Cast<OneLiteral>())
                {
                    str += item.LiteralAlternativeStr + "\n";
                }

                if (str.Length > 0)
                    str = str.Substring(0, str.Length - 1);

                return str;
            }
        }

    }
}
